import axios from 'axios'
import store from '@/store'

const service = axios.create ({
	baseURL: process.env.VUE_APP_BASE_API,
	timeout: 1000*60,
	headers: {'content-type': 'application/json'}
})

service.interceptors.request.use (config => {
	let urlList = ['user/login', 'user']
	config.baseURL += '/invoicing/'
	if (urlList.indexOf (config.url) === -1) {
		let auth = store.getters.getAuth
		config.headers['X-HEADER-TOKEN'] = auth.token
		config.headers['X-HEADER-USER-ID'] = auth.userId
	}
	return config

})
service.interceptors.response.use (res => {
		const code = res.data.code
		let {data, message} = res.data
		return code === 500 ? Promise.reject (message) : data
	}
)


export default service
